<template>
  <button class="g-button" :class="{[`icon-${iconPostion}`]:true}" @click="$emit('click')">
    <sy-icon class="icon" v-if="icon&&!loading" :name="icon"></sy-icon>
    <sy-icon class="loading icon" v-if="loading" name="loading"></sy-icon>
    <div class="content">
      <slot></slot>
    </div>
  </button>
</template>

<script>
import SyIcon from './icon'
export default {
  name: 'SyButtone',
  components: {
    SyIcon
  },
  props: {
    icon: { type: String },
    loading: { type: Boolean, default: false },
    iconPostion: {
      type: String,
      default: 'left',
      validator(value) {
        return value === 'left' || value === 'right'
      }
    }
  }
}
</script>

<style lang="scss">
@keyframes spin {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}
.g-button {
  display: inline-flex;
  justify-content: center;
  align-items: center;
  vertical-align: middle;
  font-size: var(--font-size);
  height: var(--button-height);
  line-height: var(--button-height);
  padding: 0 1rem;
  border-radius: var(--border-radius);
  border: 1px solid var(--border-color);
  background: var(--button-bg);
  &:hover {
    border-color: var(--border-color-hover);
  }
  &:active {
    background-color: var(--button-active-bg);
  }
  &:focus {
    outline: none;
  }
  > .content {
    order: 2;
  }
  > .icon {
    order: 1;
    margin-right: 0.3em;
  }
  &.icon-right {
    > .content {
      order: 1;
    }
    > .icon {
      order: 2;
      margin: 0;
      margin-left: 0.3em;
    }
  }
  .loading {
    animation: spin 1s infinite linear;
  }
}
</style>
